<template>
  <list-card>
    <template v-slot:search>
      <el-form ref="queryForm" inline :model="queryParams" label-suffix="：" size="small">
        <el-form-item label="序列号名称" prop="name">
          <el-input v-model="queryParams.name" clearable placeholder="请输入序列号名称"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item v-if="showSearch" label="最后生成时间" prop="lastTime">
          <el-date-picker v-model="queryParams.lastTime" :default-time="['00:00:00', '23:59:59']" clearable
                          type="daterange" unlink-panels range-separator="-" start-placeholder="开始时间"
                          end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="showSearch" label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option v-for="item in localDict.system_status" :key="item.value" :label="item.label"
                       :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" type="info" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template v-slot:operate>
      <el-button icon="el-icon-plus" size="mini" type="primary" @click="handleAdd">新增</el-button>
      <el-button :disabled="multiple" icon="el-icon-delete" size="mini" @click="handleDelete">删除</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
    </template>
    <template v-slot:content>
      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="50"/>
        <el-table-column label="序列号名称" prop="name" min-width="150" show-overflow-tooltip/>
        <el-table-column label="业务名称" min-width="120px" prop="busCode">
          <template v-slot:default="scope">
            <dict-tag :options="dict.type.bus_serial_code" :value="scope.row.busCode" show-label/>
          </template>
        </el-table-column>
        <el-table-column label="序列号代码" prop="code" min-width="150"/>
        <el-table-column label="表达式" prop="template" min-width="160px" show-overflow-tooltip/>
        <el-table-column label="最后生成时间" prop="lastTime" width="160px"/>
        <el-table-column label="状态" prop="status" width="100px">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.system_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column class-name="small-padding fixed-width" fixed="right" label="操作" min-width="140px">
          <template v-slot:default="scope">
            <el-button icon="el-icon-edit" size="mini" type="text" @click="handleEdit(scope.row)">修改</el-button>
            <el-button icon="el-icon-delete" size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageParam.total>0" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNum"
                  :total="pageParam.total" @pagination="getList"/>
    </template>
    <serial-model ref="model" @ok="getList"/>
  </list-card>
</template>

<script>
import {list, remove} from "@/api/base";
import {getStaticData} from "../../../api/common";
import listMixin from "../../../mixins/listMixin";
import SerialModel from "./components/ModelCard.vue";

export default {
  name: "ConfigSerial",
  dicts: ['bus_serial_code'],
  mixins:{listMixin},
  components: {SerialModel},
  data() {
    return {
      // 遮罩层
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      loading: true,
      baseUrl: '/config/serial',
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 短消息表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        name: undefined,
        staus: undefined,
        lastTime: [],
      },
      pageParam: {
        // 总条数
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      localDict: {
        system_status: [],
      },
    };
  },
  created() {
    this.getLocalDict()
    this.resetQuery();
  },
  methods: {
    getLocalDict() {
      this.getStaticData('system-status').then(res => {
        this.localDict.system_status = res.data
      })
    },
    /** 查询单编码规则配置列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
        this.dataList = response.rows;
        this.pageParam.total = response.total;
        this.loading = false;
      });
    },
    // /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.pageNum = 1;
    //   this.getList();
    // },
    // /** 重置按钮操作 */
    // resetQuery() {
    //   this.resetForm("queryForm");
    //   this.handleQuery();
    // },
    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.id)
    //   this.single = selection.length !== 1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.model.add()
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.$refs.model.edit(row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除选中的数据吗？').then(() => {
        const ids = row.id || this.ids;
        remove(this.baseUrl, ids, ids.length > 1).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(error => {
          this.$modal.msgError(error.message);
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
